package hmvictor.jpql.nodes;

import hmvictor.jpql.Connection;
import hmvictor.jpql.ConnectionDialog;
import hmvictor.jpql.ConnectionsTopComponent;
import java.awt.event.ActionEvent;
import java.util.Set;
import javax.swing.AbstractAction;
import org.openide.nodes.Node;
import org.openide.windows.WindowManager;

/**
 *
 * @author Victor
 */
public class EditConnectionAction extends AbstractAction {
    private final Node node;

    public EditConnectionAction(Node node) {
        super("Edit");
        this.node=node;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Connection connection = node.getLookup().lookup(Connection.class);
        ConnectionsTopComponent c=(ConnectionsTopComponent) WindowManager.getDefault().findTopComponent("ConnectionsTopComponent");
        Set<String> usedNames = c.getConnectionNames();
        usedNames.remove(connection.getName());
        Connection updatedConnection = ConnectionDialog.showEditDialog(WindowManager.getDefault().getMainWindow(), connection, usedNames);
        if (updatedConnection != null) {
            c.updateConnection(connection, updatedConnection);
        }
    }

}
